<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Catelist.php 138 2009-05-25 06:42:38Z movoin $
 */

function _ctlCatelist($name, $args=null)
{
    $opts = array('pid', 'ctl');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);
    //缓存过期时间
    $deploy = defined('DEPLOY_MODE') && DEPLOY_MODE;
    if ( $deploy ) {
        $lifetime = 86400; //一天
    } else {
        $lifetime = 0;
    }
    //缓存名称
    $cache_id = 'catelist.' . $name;
    //从缓存中读取数据
    $cates = FLEA::getCache($cache_id, $lifetime, true, true);
    if(!is_array($cates)) {
        //载入分类表入口
        $tblCategories =& FLEA::getSingleton( 'Table_Categories' );
        $where[] = array('col_key', $name);
        $cates['list'] = $tblCategories->findAll($where, 'sort_id ASC, cate_id ASC', null, 'cate_id, parent_id, name');
        FLEA::loadFile('Helper_Common');
        $cates['compose'] = composeCategories($cates['list']);
        FLEA::writeCache( $cache_id, $cates, true );
    }
    $_cate = findCategory($cates['list'], (int)$data['pid']);
    $output = '';
    if($cates['compose']) {
        foreach($cates['compose'] as $cate) {
            $output .= '<li><a href="' . url($data['ctl'], 'list', array('class'=>$cate['cate_id'])) . '">' . $cate['name'] . '</a></li>';

            if($cate['cate_id']==$data['pid'] || $cate['cate_id']==$_cate['parent_id']) {
                if($cate['child']) {
                    foreach($cate['child'] as $child) {
                        $output .= '<li class="nav-child"><a href="' . url($data['ctl'], 'list', array('class'=>$child['cate_id'])) . '">' . $child['name'] . '</a></li>';
                    }
                }
            }

        }
    }
    return $output;
}
